package Web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import Init.Init;
import Main.main;

@Path("/")
public class index {
	@GET
	@Produces("text/html")
    public String get() throws Exception {
		File logFile=main.menu.log.fileme();
		if(logFile!=null){
			logFile.deleteOnExit();
		}
		String ret=	"<html><head>\n";
		ret+="<style type=\"text/css\">\n";
		ret+="form{margin:0;}\n";
		ret+="form{padding:0;}\n";
		ret+="form {display: inline;}\n";
		ret+="td{font: 8pt Arial;}\n";
		ret+="td.green{background-color:#AAFFAA;}\n";
		ret+="td.red{background-color:red;}\n";
		ret+="</style>\n";
		ret+="<script type=\"text/JavaScript\">\n"+
			"<!--\n"+
			"function timedRefresh(timeoutPeriod) {\n"+
			"setTimeout(\"location.reload(true);\",timeoutPeriod);\n"+
			"}\n"+
			"-->\n"+
			"</script>\n";
		ret+="</head>\n";
		ret+="<body onload=\"JavaScript:timedRefresh(500);\"><center><table>";
		ret+="<tr align=center><td><h1><u>Web User Interface</u></h1></td></tr>\n";
		ret+="<tr><th><u>Turn Listening on and off:</u></th></tr>";
		ret+="<tr align=center><td><form action=\"http://localhost:8080/listen\" method=\"post\">"+
		"<input type=\"submit\" value=\"Toggle\"></form>"+
		"</td></tr>";
		ret+="<tr><th><u>Initialization process:</u></th></td></tr>"+
				"<tr><td align=center>"+
				"<form action=\"http://localhost:8080/initiation\" method=\"post\">"+
				"<input type=\"submit\" value=\"Start\"></form>"+
				"<form action=\"http://localhost:8080/pausing\" method=\"post\">"+
				"<input type=\"submit\" value=\"Pause\"></form>"+
				"<form action=\"http://localhost:8080/stoping\" method=\"post\">"+
				"<input type=\"submit\" value=\"Stop\">"+
				"</form></td></tr></table>";
		ret+= "<table width='200' cellspacing='1' cellpadding='0' border='1'>";
		ret+= "<tr>";
		int progress=0;
		if(main.menu.initon){
			progress=Init.getInstance().state;	
		}
		for( int ix = 0; ix <100; ix=ix+10 ){
			if( ix <progress )
				ret+= "<td class='green' nowrap='nowrap'>&nbsp;</td>";
			else
				ret+= "<td class='red' nowrap='nowrap'>&nbsp;</td>"; 
		}
		ret+= "</tr>";
		ret+= "</table>";
		ret+="<h4><u>Log:</u></h4>";
		ret+="<table border=1>";
		if(logFile!=null){
			BufferedReader br=new BufferedReader( new FileReader(logFile));
			String line;
			while((line=br.readLine())!=null){
				ret+="<tr><td>"+line+"</td></tr>";
			}
			br.close();
		}
		ret+="</table>";
		ret+="</center></body></html>";
		return ret;
    }

}
